package com.hxf.web.action.appraisal;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.GlobalMPURL;
import com.hxf.common.PublicUtil;
import com.hxf.dto.AppraisalDto;
import com.hxf.dto.ReturnData;
import com.hxf.exception.DaoLevelException;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

/*
 * 评价 action
 */
@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class AppraisalAction extends WebActionBase{

	private static final long serialVersionUID = 1L;
	public AppraisalDto appraisalDto;
	public List<AppraisalDto> appraisalList;
	private Integer id;
	private String appraisalInfo;
	private Integer score;
	private String appUserName;
	private Integer status;
	private Integer appraisalType;
	private Integer appraisalTypeId;
	private String userIds;
	
	public String selectAppraisal() throws ServiceException{
		
		Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
		if(null != restaurentUserDto){
			appraisalDto.setRestaurantId(getRestaurantDto().getId());
		}
		
		ReturnData<AppraisalDto> data = (ReturnData<AppraisalDto>)super.serviceFacade.getAppraisalService().findAppraisal(appraisalDto, targetPage, pageSize);
		appraisalList = data.getResultlist();
		this.setRecordCount(data.getReturnResult());
		
		return "appraisal-list";
	}
	
	/**
	 * 后台管理 - 评价
	 * @return
	 * @throws ServiceException
	 */
	public String adminSelectAppraisal() throws ServiceException{
		
		Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
		if(null != restaurentUserDto){
			appraisalDto.setRestaurantId(getRestaurantDto().getId());
		}
		
		ReturnData<AppraisalDto> data = (ReturnData<AppraisalDto>)super.serviceFacade.getAppraisalService().findAppraisal(appraisalDto, targetPage, pageSize);
		appraisalList = data.getResultlist();
		this.setRecordCount(data.getReturnResult());
		
		return "appraisal-admin";
	}
	
	/**
	 * 修改评价操作
	 * @return
	 * @throws Exception
	 */
	public String updateAppraisal() throws Exception{
		
		String id = ServletActionContext.getRequest().getParameter("id");//评价的编号
		if(PublicUtil.isNotNullAndEmpty(id)){
			appraisalDto = (AppraisalDto)super.serviceFacade.getMealBoxService().get(AppraisalDto.class, Integer.valueOf(id));
			getSession().setAttribute("appraisalDto", appraisalDto);
			
		}

		return "update-appraisal";
	}
	
	/**
	 * 删除评价信息
	 * @return
	 */
	public String appraisalDelete() {
		try {
			AppraisalDto deleteAppraisal = new AppraisalDto();
			deleteAppraisal.setId(id);
			super.serviceFacade.getAppraisalService().delete(deleteAppraisal);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	/*
	 * 修改
	 */
	public String saveAppraisal(){
		try{
			appraisalDto = (AppraisalDto) getSession().getAttribute("appraisalDto");
			System.out.println("dto:"+appraisalDto.toString());
			appraisalDto.setId(id);
			appraisalDto.setAppraisalInfo(appraisalInfo);
			appraisalDto.setAppUserName(appUserName);
			appraisalDto.setScore(score);
			appraisalDto.setCreateDate(DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M));
			
			if(PublicUtil.isNotNull(id)){
				super.serviceFacade.getAppraisalService().update(appraisalDto);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		requestURL = "appraisal!selectAppraisal.html";
		return SUCCESS;
	}

	/**
	 * 修改会员状态
	 * @return
	 * @throws DaoLevelException 
	 */
	public String updateUserStatus() throws DaoLevelException {
		//菜单权限验证
		if (!menuPrivilegeValidate(GlobalMPURL.APP_USER_URL)) {
			return "msg";
		}  
		try {
			if ("".equals(userIds) || null == userIds || status == null) {
				println(-1);
				return null;
			}
			//super.serviceFacade.getAdminUserService().updateUserStatus(userIds, status);
			super.serviceFacade.getAppraisalService().updateUserStatus(userIds, status);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public AppraisalDto getAppraisalDto() {
		return appraisalDto;
	}

	public void setAppraisalDto(AppraisalDto appraisalDto) {
		this.appraisalDto = appraisalDto;
	}

	public List<AppraisalDto> getAppraisalList() {
		return appraisalList;
	}

	public void setAppraisalList(List<AppraisalDto> appraisalList) {
		this.appraisalList = appraisalList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getAppraisalInfo() {
		return appraisalInfo;
	}

	public void setAppraisalInfo(String appraisalInfo) {
		this.appraisalInfo = appraisalInfo;
	}

	public Integer getScore() {
		return score;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	public String getAppUserName() {
		return appUserName;
	}

	public void setAppUserName(String appUserName) {
		this.appUserName = appUserName;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Integer getAppraisalType() {
		return appraisalType;
	}

	public void setAppraisalType(Integer appraisalType) {
		this.appraisalType = appraisalType;
	}

	public Integer getAppraisalTypeId() {
		return appraisalTypeId;
	}

	public void setAppraisalTypeId(Integer appraisalTypeId) {
		this.appraisalTypeId = appraisalTypeId;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}
}
